<?php

namespace app\controllers; //命名空间

use yii\web\Controller; // 导入Controller
use app\controllers\BaseController; //导入基础controller（内含各种基础使用函数）
use app\common\Deal;  //导入微信登陆处理类
use app\models\User; // 用户模型
use Yii; // 导入Yii
use app\common\Util;  // 导入公共方法


// AppletController 控制器类
class AppletController extends Controller{
        public $enableCsrfValidation = false;
	//小程序进行登陆需要code进行测试看看是否已登陆
	public function actionLogin(){
		$wxdeal = new Deal();
		$redis = Yii::$app->redis;
        if( Yii::$app->request->isPost ){
            $postData = Yii::$app->request->post(); //获取code session
        	$code = $postData['code']; //获取小程序所给的code
                $session = $postData['session'];
            
            //判断code是否存在
            if(empty($code)){
                $content = "没有找到code,请尝试重新登录";
                $data = array(
                    '0' => -3,
                    '1' => $content,
                );
                return BaseController::FailRespone($data);
                exit(0);
            }
            
        	if( empty($session) ){
        		$logininfo = Yii::$app->redis->del('3rdsession'.$session);
        	}
        	//获取用户的openid
        	$logininfo = $wxdeal->getLogininfo($code);
        	$session_key = $logininfo['session_key'];
                $openid = $logininfo['openid'];

            //检查session_key是否存在
            if( !isset($session_key) ){
                $content = "没有找到session_key,请尝试重新登录";
                $data = array(
                    '0' => -3,
                    '1' => $content,
                );
                return BaseController::FailRespone($data);
                exit(0);
            }

            //redis储存用户信息
            $redisinfo = array(
                'openid' => $openid,
                'session_key' =>  $session_key,
            );
            $session = $wxdeal->_3rd_session(32);
            $key = '_3rd_session:' . $session;
            $redis->setex($key, 1209600, json_encode($redisinfo));
            
            //传送openid
            $data = array(
                'login' => true, //已经登录
                'session' => $session,
                'openid' => $openid,
            );
            return BaseController::SuccessRespone($data);
            exit(0);
        }else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);   
        }
        exit(0);
	}

	// 向微信发起请求获取openId 和 session_key
	public function actionGetopenid(){

		if(Yii::$app->request->isPost){
			// 获取数据
			$postData =  Yii::$app->request->post();
			
			$grant_type = 'authorization_code';
			$APPID = Yii::$app->params['WECHAT']['APPID']; 
			$APPSECRET = Yii::$app->params['WECHAT']['APPSECRET'];

			$url_openId = Yii::$app->params['URL']['OPENID'].'?appid='.$APPID.'&secret='.$APPSECRET.'&js_code='.$postData['js_code'].'&grant_type='.$grant_type; 
			$html_openId = Util::getSSL($url_openId);
			$html_openId = json_decode($html_openId, true);
		
			$data = array(
				'openId' => $html_openId['openid'],
				'session_key' => $html_openId['session_key']
			);
			
			echo json_encode( array('error'=>0,'data'=>$data) );
		}else{
			echo json_encode( array('error'=>1,'data'=>'提交数据方式有误') );   
		}
	}

}
